﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommonHelper.Model;
using CommonHelper.Model.Qy;
using CommonHelper.Utility;
using Newtonsoft.Json;

namespace CommonHelper.Server.Qy
{
    public class DepartmentService
    {
        /// <summary>
        /// 创建部门Api
        /// </summary>
        private static string CreateApi = "https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token={0}";
        /// <summary>
        /// 更新部门Api
        /// </summary>
        private static string UpdateApi = "https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token={0}";
        /// <summary>
        /// 删除部门Api
        /// </summary>
        private static string DeleteApi = "https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token={0}&id={1}";
        /// <summary>
        /// 获取部门列表Api
        /// </summary>
        private static string GetListApi = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={0}&id={1}";

        /// <summary>
        /// 创建部门
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="department"></param>
        public static WxJsonResult CreateDepartMent(string accessToken, DepartmentResult department)
        {
            var url = string.Format(CreateApi, accessToken);

            //序列化实体为json
            string json = JsonConvert.SerializeObject(department, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

            //调用消息发送接口
            return HttpClientHelper.PostResponse<WxJsonResult>(string.Format(url, accessToken), json);
        }

        /// <summary>
        /// 获取部门列表
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static GetDepartmentList GetDepartmentList(string accessToken, string id)
        {
            var url = string.Format(GetListApi, accessToken, id);

            var dept = HttpClientHelper.GetResponse<GetDepartmentList>(url);

            return dept;
        }

        public static WxJsonResult DeletePartment(string accessToken,string id)
        {
            var url = string.Format(DeleteApi, accessToken, id);
            return HttpClientHelper.GetResponse<WxJsonResult>(url);
        }
    }

}
